<template>
  <EcDialog v-model="dialogVisible" :auto-height="true" class="form-dialog">
    <!-- 标题 -->
    <template #title>
      <div class="dialog-title">
        <SvgIcon :icon="dialogIconMap[dialogMode]"/>
        <span>{{ dialogTitle }}</span>
      </div>
    </template>

    <!-- 表单数据 -->
    <el-form ref="formRef"
             v-loading="dataLoading"
             :model="formData"
             :rules="formRules"
             label-width="120px">

      <el-form-item label="商户类型：">
        <DictTag v-model="formData.shop_type" :circle="false" dict-type="shop_type"/>
      </el-form-item>

      <el-form-item label="商户名称：">
        <span>{{ merchantData.shop_name }}</span>
      </el-form-item>

      <el-form-item label="商户余额：">
        <span>{{ toDecimal(merchantData.balance, 4) }}</span>
      </el-form-item>

      <el-form-item label="结算金额：" prop="amount">
        <el-input-number v-model="formData.amount" :min="0" :precision="4" :step="0.01" controls-position="right" placeholder="请输入结算金额"/>
      </el-form-item>
    </el-form>

    <!-- 底部操作区域 -->
    <template #footer>
      <span class="dialog-footer">
        <el-button :loading="dataLoading" @click="dialogVisible = false">取 消</el-button>
        <el-button v-permission="['merchant:settlement:operations']" :loading="dataLoading" type="primary" @click="onSubmitForm()">确 定</el-button>
      </span>
    </template>
  </EcDialog>
</template>

<script lang="ts" setup>
import { computed, reactive, ref } from 'vue';
import { ElLoading, ElNotification, FormInstance, FormRules } from 'element-plus';
import { EcDialog } from '@/components/dialog';
import { SvgIcon } from '@/components/icon';
import useDialog from '@/hooks/use-dialog.ts';
import { merchantSettlementApply } from '@/api/api-merchant.ts';
import { DictTag } from '@/components/dict-tag';
import { toDecimal } from '@/utils';

defineOptions({name: 'settlement-apply-form'})

const formRef = ref<FormInstance>();
const formLoading = ref(false); // 表单是否加载中
const {dialogVisible, dialogTitle, dialogMode, dialogModeMap, dialogIconMap} = useDialog(false);

/** 默认表单数据 */
export type SettlementApply = {
  shop_id: number;
  shop_type: number;
  amount: number;
};
const defaultForm: SettlementApply = <SettlementApply>{};

// 表单数据
const formData = ref({...defaultForm});
const merchantData = ref();

// 表单验证规则
const formRules = reactive<FormRules>({
  amount: [{required: true, message: '结算金额不能为空', trigger: 'blur'}],
});

/** 判断数据是否处于加载中 */
const dataLoading = computed(() => {
  return formLoading.value;
})

/** 打开弹窗 */
const open = async (item: any) => {
  dialogMode.value = 1;
  dialogTitle.value = '商户结算申请';
  dialogVisible.value = true;
  // 重置表单
  handleFormReset();
  merchantData.value = item;
  formData.value.shop_id = item.shop_id
  formData.value.shop_type = item.shop_type
  formLoading.value = false
}
// 提供 open 方法，用于打开弹窗
defineExpose({open});

/** 表单提交 */
const emit = defineEmits(['refresh']);
const onSubmitForm = async () => {
  await formRef.value?.validate((valid, fields) => {
    if (!valid) return;

    const loading = ElLoading.service({
      lock: true,
      text: '正在提交中...',
      background: 'rgba(0, 0, 0, 0.2)'
    })
    // 提交数据
    merchantSettlementApply(formData.value).then(response => {
      // 提示信息
      ElNotification({title: '成功', message: '操作完成', type: 'success', duration: 2000})
      // 发送操作成功的事件
      emit('refresh')
      // 隐藏窗口
      dialogVisible.value = false
    }).finally(() => loading.close())

  });
};

/** 重置表单 */
const handleFormReset = () => {
  // 重置表单数据
  formData.value = {...defaultForm};
  // 重置界面表单数据
  formRef.value?.resetFields();
};

</script>

<style lang="scss" scoped>
.form-dialog {
  .el-select,
  .el-input-number {
    max-width: 13rem;
  }
}
</style>
